package com.yidu.DailyFinalTreatment.controller;

import com.yidu.BusinessParameters.entity.CashAccruedEntity;
import com.yidu.BusinessParameters.service.CashAccruedEntityService;
import com.yidu.DailyFinalTreatment.entity.FundTwoFees;
import com.yidu.DailyFinalTreatment.mapper.FundTwoFeesMapper;
import com.yidu.DailyFinalTreatment.service.FundTwoFeesService;
import com.yidu.Util.util.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FundTwoFeesController {
    @Resource
    private FundTwoFeesService fundTwoFeesService;
    @Resource
    private CashAccruedEntityService accruedEntityService;
    @Resource
    private Dbutil dbUtil;

    @RequestMapping("fundTwoFees")
    public Map fundTwoFeesSelect(String date){
        System.out.println("123");
       List<FundTwoFees> fundTwoFeesList =fundTwoFeesService.fundTwoFeesSelect(date);
        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("data",fundTwoFeesList);
        System.out.println(map.get("data"));
        System.out.println("两费"+fundTwoFeesList);
        return map;

    }
    //两费利息
    @RequestMapping("funFeesTwo")
    public int statisticalTwoFees(String FundTwoFees) {
        int i=0;
        System.out.println("进来了===============================================");
        System.out.println("TwoFees"+FundTwoFees);
        //转json格式
        List<FundTwoFees> fundTwoFeesList = SysUtil.jsonToArrayList(FundTwoFees,FundTwoFees.class);
        for (FundTwoFees fundTwoFees:fundTwoFeesList) {
            //现金应收应付
            CashAccruedEntity cashAccruedEntity = new CashAccruedEntity();
            //给日期赋值
            cashAccruedEntity.setCashDate(fundTwoFees.getNetWorthDate());
            //基金id赋值
            cashAccruedEntity.setFundId(fundTwoFees.getFundId());
            //现金id赋值
            cashAccruedEntity.setCashId("C20220628001");
            //业务流向
            cashAccruedEntity.setBusinessType(1);
            if(cashAccruedEntity.getBusinessType()==1){
                //现金应收应付id自动增长
                String cashAccruedId = dbUtil.getMaxId3(TableIdUtil.CAID, TableNameUtil.CA, TableTimeUtil.CATime, cashAccruedEntity.getCashDate());
                cashAccruedEntity.setCashAccruedId(cashAccruedId);
                //托管费利息
                cashAccruedEntity.setMoneySun(fundTwoFees.getCustodianInterest());
                //资金流向
                cashAccruedEntity.setMoneyFlow(1);
                //默认增加
                int il =accruedEntityService.cashInventoryinsert(cashAccruedEntity);
                if (il>0){
                    //删除
                    il=accruedEntityService.cashDeleteNew(cashAccruedEntity);
                    if (il>0){
                        //增加
                        accruedEntityService.cashInventoryinsert(cashAccruedEntity);
                    }
                }
            }

            cashAccruedEntity.setBusinessType(2);
            if(cashAccruedEntity.getBusinessType()==2){
                String cashAccruedId = dbUtil.getMaxId3(TableIdUtil.CAID, TableNameUtil.CA, TableTimeUtil.CATime, cashAccruedEntity.getCashDate());
                cashAccruedEntity.setCashAccruedId(cashAccruedId);
                //定义管理费
                double bankInterest=fundTwoFees.getBankInterest();
                //管理费赋值
                cashAccruedEntity.setMoneySun(bankInterest);
                int io=accruedEntityService.cashInventoryinsert(cashAccruedEntity);
                if (io>0){
                    io=accruedEntityService.cashDeleteNew(cashAccruedEntity);
                    if (io>0){
                        i=accruedEntityService.cashInventoryinsert(cashAccruedEntity);
                    }
                }
            }

        }
        return i;
    }
}
